package com.egao.orderB.biz.impl;

import com.egao.common.core.web.BaseDao;
import com.egao.common.core.web.PageResult2;
import com.egao.common.core.utils.DateUtil;
import com.egao.common.core.utils.Utils;
import com.egao.common.system.entity.User;
import com.egao.common.core.constants.UserConstant;
import com.egao.orderB.biz.CustomerBiz;
import com.egao.orderB.entity.Customer;
import com.egao.orderB.entity.CustomerCopy;
import net.sf.json.JSONObject;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2020-06-02 13:34:13
 * @version ：1.0
 */
@Repository
public class CustomerBizImpl extends BaseDao implements CustomerBiz {

	@Override
	public JSONObject list(PageResult2<T> page, Customer customer, User loginUser) throws Exception {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<Customer> rm = new BeanPropertyRowMapper<Customer>(Customer.class);
		StringBuffer sql = new StringBuffer("select c.id,c.company_id,c.name");
        sql.append(",c.admin_user,c.code,c.social_code,c.business_license,c.salesman,c.settle");
		sql.append(",c.contacts,c.contacts_phone,c.address");
		sql.append(",c.state,c.order_num,c.create_time,c.create_user");
		sql.append(",c.update_time,c.update_user");
        sql.append(",cmy.name companyName,suser.username createName ");
        sql.append(" from chf_customer c ");
        sql.append(" inner join sys_user suser on suser.user_id=c.create_user ");
        sql.append(" inner join chf_company cmy on cmy.id=c.company_id ");
        sql.append(" where c.deleted=0 and cmy.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
            sql.append(" and c.company_id=:tablecompanyId ");
            map.put("tablecompanyId", loginUser.getCompanyId() + "");
        }
    	if(Utils.isNotEmpty(customer)){
			if(Utils.isNotEmpty(customer.getState())){
				sql.append(" and instr(c.state,:state)>0 ");
				map.put("state", customer.getState());
			}
			if(Utils.isNotEmpty(customer.getSalesman())){
				sql.append(" and instr(c.salesman,:salesman)>0 ");
				map.put("salesman", customer.getSalesman());
			}
			if(Utils.isNotEmpty(customer.getSettle())){
				sql.append(" and instr(c.settle,:settle)>0 ");
				map.put("settle", customer.getSettle());
			}
			if(Utils.isNotEmpty(customer.getName())){
				sql.append(" and instr(c.name,:name)>0 ");
				map.put("name", customer.getName());
			}
			if(Utils.isNotEmpty(customer.getSort())&&Utils.isNotEmpty(customer.getOrder())){
				sql.append(" order by "+customer.getSort()+" "+customer.getOrder());
			} else {
				sql.append(" order by c.create_time desc");
			}
		}
		return jsonPage2(getPageQueryList(page, sql.toString(), map, rm));
	}

	@Override
	public List<Customer> list(Customer customer, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<Customer> rm = new BeanPropertyRowMapper<Customer>(Customer.class);
        StringBuffer sql = new StringBuffer("select c.id,c.company_id,c.name");
        sql.append(",c.admin_user,c.code,c.social_code,c.business_license,c.salesman,c.settle");
		sql.append(",c.contacts,c.contacts_phone,c.address");
		sql.append(",c.state,c.order_num,c.create_time,c.create_user");
		sql.append(",c.update_time,c.update_user");
        sql.append(",cmy.name companyName,suser.username createName ");
        sql.append(" from chf_customer c ");
        sql.append(" inner join sys_user suser on suser.user_id=c.create_user ");
        sql.append(" inner join chf_company cmy on cmy.id=c.company_id ");
        sql.append(" where c.deleted=0 and cmy.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
            sql.append(" and c.company_id=:tablecompanyId ");
            map.put("tablecompanyId", loginUser.getCompanyId() + "");
        }
		if(Utils.isNotEmpty(customer)){
			if(Utils.isNotEmpty(customer.getState())){
				sql.append(" and instr(c.state,:state)>0 ");
				map.put("state", customer.getState());
			}
			if(Utils.isNotEmpty(customer.getSalesman())){
				sql.append(" and instr(c.salesman,:salesman)>0 ");
				map.put("salesman", customer.getSalesman());
			}
			if(Utils.isNotEmpty(customer.getSettle())){
				sql.append(" and instr(c.settle,:settle)>0 ");
				map.put("settle", customer.getSettle());
			}
			if(Utils.isNotEmpty(customer.getName())){
				sql.append(" and instr(c.name,:name)>0 ");
				map.put("name", customer.getName());
			}
			if(Utils.isNotEmpty(customer.getSort())&&Utils.isNotEmpty(customer.getOrder())){
				sql.append(" order by "+customer.getSort()+" "+customer.getOrder());
			} else {
				sql.append(" order by c.create_time desc");
			}
		}
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public Customer getById(Customer customer) {
		RowMapper<Customer> rm = new BeanPropertyRowMapper<Customer>(Customer.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select c.id,c.company_id,c.name");
        sql.append(",c.admin_user,c.code,c.social_code,c.business_license,c.salesman,c.settle");
		sql.append(",c.contacts,c.contacts_phone,c.address");
		sql.append(",c.state,c.order_num,c.create_time,c.create_user");
		sql.append(",c.update_time,c.update_user");
        sql.append(",cmy.name companyName,suser.username createName ");
        sql.append(" from chf_customer c ");
        sql.append(" inner join sys_user suser on suser.user_id=c.create_user ");
        sql.append(" inner join chf_company cmy on cmy.id=c.company_id ");
        sql.append(" where c.deleted=0 and cmy.deleted=0 and c.id=:id ");
		map.put("id", customer.getId());
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}

	@Override
	public List<Customer> getByName(Customer customer) {
		RowMapper<Customer> rm = new BeanPropertyRowMapper<Customer>(Customer.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select c.id,c.company_id,c.name");
        sql.append(",c.admin_user,c.code,c.social_code,c.business_license,c.salesman,c.settle");
		sql.append(",c.contacts,c.contacts_phone,c.address");
		sql.append(",c.state,c.order_num,c.create_time,c.create_user");
		sql.append(",c.update_time,c.update_user");
        sql.append(",cmy.name companyName,suser.username createName ");
        sql.append(" from chf_customer c ");
        sql.append(" inner join sys_user suser on suser.user_id=c.create_user ");
        sql.append(" inner join chf_company cmy on cmy.id=c.company_id ");
        sql.append(" where c.deleted=0 and cmy.deleted=0 and c.name=:name ");
		map.put("name", customer.getName());
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(), map, rm);
	}

	@Override
	public boolean save(Customer customer, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into chf_customer (");
        sql.append("company_id,name,admin_user,code,social_code,business_license");
		sql.append(",contacts,contacts_phone,address");
		sql.append(",salesman,settle,state,order_num");
		sql.append(",create_user,create_time) values ( ");
        sql.append(":company_id,:name,:code,:social_code,:business_license");
		sql.append(",:contacts,:contacts_phone,:address");
		sql.append(",:salesman,:settle,:state,:order_num");
		sql.append(",:create_user,now())");
        map.put("company_id", loginUser.getCompanyId() + "");
        map.put("name", customer.getName());
        map.put("admin_user", customer.getAdminUser());
        map.put("code", customer.getCode());
        map.put("social_code", customer.getSocialCode());
        map.put("business_license", customer.getBusinessLicense());
        map.put("contacts", customer.getContacts());
        map.put("contacts_phone", customer.getContactsPhone());
        map.put("address", customer.getAddress());
        map.put("salesman", customer.getSalesman());
        map.put("settle", customer.getSettle());
        map.put("state", customer.getState());
        map.put("order_num", customer.getOrderNum()==null?null:customer.getOrderNum()+"");
		map.put("create_user", loginUser.getUserId() + "");
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public Integer ApiSave(CustomerCopy customer, User loginUser) {
		GeneratedKeyHolder generatedKeyHolder = new GeneratedKeyHolder();
		KeyHolder keyHolder=new GeneratedKeyHolder();
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into chf_customer (");
        sql.append("company_id,name,admin_user,code,social_code,business_license");
		sql.append(",contacts,contacts_phone,address");
		sql.append(",salesman,settle,state,order_num");
		sql.append(",create_user, create_time) values ( ");
        sql.append(":company_id,:name,:admin_user,:code,:social_code,:business_license");
		sql.append(",:contacts,:contacts_phone,:address");
		sql.append(",:salesman,:settle,:state,:order_num");
		sql.append(",:create_user, now())");
        map.put("company_id", loginUser.getCompanyId() + "");
        map.put("name", customer.getName());
        map.put("admin_user", customer.getAdminUser());
        map.put("code", customer.getCode());
        map.put("social_code", customer.getSocialCode());
        map.put("business_license", customer.getBusinessLicense());
		map.put("contacts", customer.getContacts());
		map.put("contacts_phone", customer.getContactsPhone());
		map.put("address", customer.getAddress());
        map.put("salesman", customer.getSalesman());
        map.put("settle", customer.getSettle());
        map.put("state", customer.getState());
        map.put("order_num", customer.getOrderNum()==null?null:customer.getOrderNum()+"");
		map.put("create_user", loginUser.getUserId() + "");
		getMapParameter(sql, map);


//		System.err.println("受到影响的行数=" + retVal + ",
		int retVal = this.jdbcTemplate.update(sql.toString(), new SqlParameterSource() {
			@Override
			public boolean hasValue(String paramName) {
				return map.containsKey(paramName);
			}
			@Override
			public Object getValue(String paramName) throws IllegalArgumentException {
				return map.get(paramName);
			}
		}, generatedKeyHolder);

		Integer id = generatedKeyHolder.getKey().intValue();
		return id;
	}

	@Override
	public boolean update(Customer customer, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update chf_customer set ");
        sql.append("code=:code ");
		sql.append(",salesman=:salesman,settle=:settle ");
		sql.append(",contacts=:contacts,contacts_phone=:contacts_phone,address=:address");
		sql.append(",order_num=:order_num,update_user=:update_user where id=:id ");
//        map.put("name", customer.getName());
        map.put("code", customer.getCode());
//        map.put("social_code", customer.getSocialCode());
//        map.put("business_license", customer.getBusinessLicense());
        map.put("salesman", customer.getSalesman());
        map.put("settle", customer.getSettle());
        map.put("contacts", customer.getContacts());
        map.put("contacts_phone", customer.getContactsPhone());
        map.put("address", customer.getAddress());
//        map.put("state", customer.getState());
        map.put("order_num", customer.getOrderNum()==null?null:customer.getOrderNum()+"");
		map.put("update_user", loginUser.getUserId() + "");
		map.put("id", customer.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean examine(Customer customer, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update chf_customer set ");
		sql.append("state=:state ");
		sql.append(",update_user=:update_user where id=:id ");
        map.put("state", customer.getState());
		map.put("update_user", loginUser.getUserId() + "");
		map.put("id", customer.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean examineFail(Customer customer, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update chf_customer set ");
		sql.append("state=:state ");
		sql.append(",update_user=:update_user where id=:id ");
        map.put("state", customer.getState());
        map.put("order_num", customer.getOrderNum()==null?null:customer.getOrderNum()+"");
		map.put("update_user", loginUser.getUserId() + "");
		map.put("id", customer.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean remove(Customer customer, User loginUser) {
		int count = 0;
		Map<String, String> map = null;
		if(customer.getId().length()>0){
			for(String ID : customer.getId().split(",")){
				map  = new HashMap<String, String>();
				if(ID==null || ID.equals("")){ break; }
				StringBuffer sql =new StringBuffer("update chf_customer set deleted=1" +
						",update_user=:update_user, update_time=now() "
						+ " where id =:id ");
				map.put("update_user", loginUser.getUserId() + "");
				map.put("id", ID);
				getMapParameter(sql, map);
				count = jdbcTemplate.update(sql.toString(),map);
			}
		}
		return count > 0;
	}
}
